package com.bj.dljd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bj.dljd.constant.SystemConstant;
import com.bj.dljd.convent.PaymentConvert;
import com.bj.dljd.domain.PayProduct;
import com.bj.dljd.domain.PayWay;
import com.bj.dljd.models.dic.DicItem;
import com.bj.dljd.models.query.ProductConfigQuery;
import com.bj.dljd.models.query.ProductQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.models.vo.PayProductQueryVo;
import com.bj.dljd.models.vo.PayProductVo;
import com.bj.dljd.models.vo.PayWayVo;
import com.bj.dljd.service.PaymentService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private PaymentConvert paymentConvert;

    /*新增支付产品*/
    @PostMapping("/pay/product/add")
    public R addPaymentProduct(@RequestBody @Validated ProductQuery productQuery) {
        // 调用业务方法
        paymentService.addProduct(productQuery);
        return R.Ok();
    }
    /*分页查询支付产品*/
    @GetMapping("/pay/product/pageList")
    public R payment(@RequestParam  Integer pageNo){
        pageNo =  (pageNo == null || pageNo < 1) ? 1 : pageNo;

        Page<PayProduct> payProduct =
                paymentService.productPageList(pageNo, SystemConstant.PAGE_SIZE);
        // 获取总页数
        long pages = payProduct.getPages();
        // 获取查询的分页数据
        List<PayProduct> productList = payProduct.getRecords();


        List<PayProductVo> payProductVos
                = paymentConvert.convertListPayProductVo(productList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("totalPage",pages);
        map.put("list",payProductVos);

        return R.Ok(map);
    }
    /*支付产品的字典*/
    @GetMapping("/pay/product/dicList")
    public R payProductDict(){
        // 查询支付产品code 和 name
        List<DicItem> dicItemList = paymentService.getProductDicList();
        return R.Ok(dicItemList);
    }
    @GetMapping("/pay/way/dicList")
    public R payWayDicList(){
        List<DicItem> dicItemList = paymentService.getPayWayDicList();
        return R.Ok(dicItemList);
    }
    @GetMapping("/pay/type/dicList")
    public R payTypeDicList(@RequestParam String wayCode){
        if (StrUtil.isNotBlank(wayCode)){
            List<DicItem> dicItemList =
                    paymentService.getPayTypeDicList(wayCode.toUpperCase());
            return R.Ok(dicItemList);
        }
        return R.fuil("请求参数异常");
    }
    @PostMapping("/pay/product/config")
    public R payTypeConfig(@RequestBody @Validated ProductConfigQuery productConfigQuery){
        paymentService.payTypeConfig(productConfigQuery);
        return R.Ok();
    }
    /*显示支付方式列表*/
    @GetMapping("/pay/way/pageList")
    public R payWayPageList(@RequestParam(required = false) String productCode,Integer pageNo){
        pageNo = (pageNo == null || pageNo <1 ) ? 1 : pageNo;
        Page<PayWay> payWayPage
                = paymentService.pageWayPageList(productCode,pageNo,SystemConstant.PAGE_SIZE);
        long totalPage = payWayPage.getPages();
        List<PayWay> payWayList = payWayPage.getRecords();

        // 转换PayWay --->PayWayVo
        List<PayWayVo> payWayVos = paymentConvert.convertListPayWayVo(payWayList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("totalPage",totalPage);
        map.put("list",payWayVos);
        return R.Ok(map);
    }
    // 修改产品状态
    @PostMapping("/pay/product/audit")
    public R payProductStatus(@RequestParam  String productCode){
        if (StrUtil.isNotBlank(productCode)){
            paymentService.editProductStatus(productCode);
            return R.Ok();
        }
        return R.fuil("请求参数异常");
    }
    // 删除支付产品配置记录
    @PostMapping("/pay/product/remove")
    public R payProductDel(@RequestParam String productCode){
         if (StrUtil.isNotBlank(productCode)){
            paymentService.removeProduct(productCode);
            return R.Ok();
         }
         return R.fuil("请求参数异常");
    }
    // 模糊查询支付产品
    @GetMapping("/pay/product/matchname")
    public R payProductMatchName(@RequestParam String productName){
        if (StrUtil.isNotBlank(productName) && productName.length() >= 2){
            List<PayProduct> productList = paymentService.matchProductMatchName(productName);
            List<PayProductQueryVo> payProductQueryVos
                    = paymentConvert.convertListPayProductQueryVo(productList);
            return R.Ok(payProductQueryVos);
        }
        return R.fuil("请求参数异常");
    }

}
